package org.swingplus.core.factory;

import org.swingplus.core.constant.bean.ScopeConstant;
import org.swingplus.core.context.ApplicationContext;
import org.swingplus.core.exception.BeansException;
import org.swingplus.core.exception.NoSuchBeanDefinitionException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class WidgetFactroy implements BeanFactory {

	
//	public static <T> T create(Class<T> clazz)   {
//		T factory = null;
//		try {
//			factory = clazz.newInstance();
//		} catch (InstantiationException | IllegalAccessException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return factory;
//	};
	
	
	public static Object create(String className)   {
		Class c = null;
		try {
			c = Class.forName(className);
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Object factory = null;
		try {
			factory = c.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {

			Constructor cons = null;
			try {
				cons = c.getDeclaredConstructor();
				//set accessble to access private constructor
				cons.setAccessible(true);
				factory=cons.newInstance();
			} catch (NoSuchMethodException | IllegalAccessException|InvocationTargetException| InstantiationException e1) {
				e1.printStackTrace();
			}
			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
		return factory;
	};

	public static Object create(String className, String scope) {

//		if(scope.equals(ScopeConstant.singleton)) {
//		}

		Class c = null;
		try {
			c = Class.forName(className);
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Object factory = null;
		try {
			factory = c.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return factory;
	};

	public static Object create(String className, String id, String scope) {
		Object bean = null;
		switch (scope) {
		case ScopeConstant.singleton:
			bean = ApplicationContext.getBean(id, className);
			if (null != bean) {
				return bean;
			} else {
				return create(className);
			}
//			return;
		case ScopeConstant.prototype:
			return create(className);
		default:
//			break;
			return null;
		}
//		return null;
	};

	@Override
	public Object getBean(String name) throws BeansException {
		return null;
	}

	@Override
	public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
		return null;
	}

	@Override
	public <T> T getBean(Class<T> requiredType) throws BeansException {
		return null;
	}

	@Override
	public Object getBean(String name, Object... args) throws BeansException {
		return null;
	}

	@Override
	public boolean containsBean(String name) {
		return false;
	}

	@Override
	public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
		return false;
	}

	@Override
	public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
		return false;
	}

	@Override
	public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
		return false;
	}

	@Override
	public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
		return null;
	}

	@Override
	public String[] getAliases(String name) {
		return new String[0];
	}
}
